package me.baicai.weixin.identity.service.impl;

import me.baicai.utils.StringUtils;
import me.baicai.weixin.entity.gcb.PageEntity;
import me.baicai.weixin.entity.gcb.Query;
import me.baicai.weixin.entity.identity.Identity;
import me.baicai.weixin.identity.repository.AccountRepository;
import me.baicai.weixin.identity.repository.LoginRepository;
import me.baicai.weixin.identity.repository.ShopRepository;
import me.baicai.weixin.application.BasicService;
import me.baicai.weixin.application.Result;
import me.baicai.weixin.conf.IdentityKey;
import me.baicai.weixin.identity.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class ShopServiceImpl extends BasicService implements ShopService
{

    @Autowired
    private ShopRepository shopRepository;

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private LoginRepository loginRepository;

    @Override
    public Result shopIndex(String openid)
    {
        Map map = new HashMap();
        map.put("deviceCount", shopRepository.getShopDeviceCount(openid));
        map.put("orderCount", shopRepository.getShopOrderCount(openid));
        map.put("account", accountRepository.findAccountByOpenid(openid));
        map.put("todayAmount", shopRepository.findToDayAmount(openid));
        return this.success(map);
    }

    @Override
    public Result shop(String openid, String input)
    {
        if (null != input && !"".equalsIgnoreCase(input))
        {
            return this.success(shopRepository.getShopInfoByInput(openid, "%" + input + "%"));
        }
        return this.success(shopRepository.getShopInfo(openid));
    }

    @Override
    public Result incomeStatistics(String openid, String beginDate, String endDate)
    {

        return this.success(shopRepository.incomeStatistics(openid, beginDate, endDate,
            IdentityKey.shop));
    }

    @Override
    public Result orderStatistics(String openid, String date)
    {
        return this.success(shopRepository.orderStatistics(openid, date, IdentityKey.shop));
    }

    @Override
    public Result shop(Query query)
    {
        Pageable pageable = new PageEntity(query.getPage(), query.getSize(), Sort.Direction.DESC,
            "id");
        Map map = new HashMap();
        if (StringUtils.isNotBlank(query.getType()) && StringUtils.isNotBlank(query.getValue()))
        {
            switch (query.getType())
            {
                case "shopId":
                    map.put("entity",
                        shopRepository.findAllById("%" + query.getValue() + "%", pageable));
                    map.put("total",
                        shopRepository.findAllByIdCount("%" + query.getValue() + "%"));
                    return this.success(map);
                case "shopName":
                    map.put("entity",
                        shopRepository.findAllByShopnameLike("%" + query.getValue() + "%",
                            pageable));
                    map.put("total",
                        shopRepository.findAllByShopnameLikeCount("%" + query.getValue() + "%"));
                    return this.success(map);
                case "agentId":
                    map.put("entity",
                        shopRepository.findAllByAgentidLike("%" + query.getValue() + "%",
                            pageable));
                    map.put("total",
                        shopRepository.findAllByAgentidLikeCount("%" + query.getValue() + "%"));
                    return this.success(map);
                case "agentName":
                    map.put("entity",
                        shopRepository.findAllByAgentNameLike("%" + query.getValue() + "%",
                            pageable));
                    map.put("total",
                        shopRepository.findAllByAgentNameLikeCount("%" + query.getValue() + "%"));
                    return this.success(map);
            }
        }
        map.put("entity", shopRepository.findAllByPage(pageable));
        map.put("total", shopRepository.count());
        return this.success(map);
    }

    @Override
    public Result shopInfo(Query query)
    {
        Pageable pageable = new PageEntity(query.getPage(), query.getSize(), Sort.Direction.DESC,
            "id");
        if (StringUtils.isNotBlank(query.getType()) && StringUtils.isNotBlank(query.getValue()))
        {
            switch (query.getType())
            {
                case "name":
                    return this.success(
                        loginRepository.findAllByNameLikeAndType(query.getValue(), IdentityKey.shop, pageable));
                case "openid":
                    return this.success(
                        loginRepository.findAllByOpenidAndType(query.getValue(), IdentityKey.shop, pageable));
                case "id":
                    return this.success(
                        loginRepository.findAllByIdAndType(Integer.valueOf(query.getValue()), IdentityKey.shop, pageable));
            }
        }
        return this.success(loginRepository.findAllByType(IdentityKey.shop,pageable));
    }
}
